package com.study.lock;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 创建资源类
 */
class ShareResource {
    // 定义标志位
    private int flag = 1;

    // 创建lock锁，可重入锁
    private Lock lock = new ReentrantLock();

    // 创建三个condition
    private Condition c1 = lock.newCondition();
    private Condition c2 = lock.newCondition();
    private Condition c3 = lock.newCondition();

    // 打印5次，参数第几轮
    public void print5(int loop) throws InterruptedException {
        // 上锁
        lock.lock();
        try {
            while (flag != 1) {
                c1.await();
            }
            for (int i = 1; i <= 5; i++) {
                System.out.println(Thread.currentThread().getName()+" :: "+i+" : 轮数 : "+loop);
            }
            // 修改标志位
            flag = 2;
            // 通知
            c2.signal();
        } finally {
            lock.unlock();
        }
    }

    // 打印10次，参数第几轮
    public void print10(int loop) throws InterruptedException {
        // 上锁
        lock.lock();
        try {
            if (flag != 2) {
                c2.await();
            }
            for (int i = 1; i <= 10; i++) {
                System.out.println(Thread.currentThread().getName()+" :: "+i+" : 轮数 : "+loop);
            }
            //修改标志位
            flag = 3;
            // 通知
            c3.signal();
        } finally {
            lock.unlock();
        }
    }

    // 打印15次，参数第几轮
    public void print15(int loop) throws InterruptedException {
        lock.lock();
        try {
            if (flag != 3) {
                c3.await();
            }
            for (int i = 1; i <= 15; i++) {
                System.out.println(Thread.currentThread().getName()+" :: "+i+" : 轮数 : "+loop);
            }
            // 修改标志位
            flag = 1;
            c1.signal();
        } finally {
            lock.unlock();
        }
    }

}

/**
 * @author wanghao
 * @version v1.0
 * @description: 线程间定制化通信
 * @date 2021/12/27 10:13
 * @change [修改时间] [修改者]@[版本号] [修改内容说明]
 */
public class ThreadDemo3 {

    public static void main(String[] args) {
        ShareResource share = new ShareResource();

        new Thread(()->{
            for (int i = 1; i <= 10; i++) {
                try {
                    share.print5(i);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"AA").start();

        new Thread(()->{
            for (int i = 1; i <= 10; i++) {
                try {
                    share.print10(i);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"BB").start();

        new Thread(()->{
            for (int i = 1; i <= 10; i++) {
                try {
                    share.print15(i);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"CC").start();
    }

}
